package com.example.springboot.controller;

import com.example.springboot.dto.SelfServicePointDTO;
import com.example.springboot.entity.SelfServicePoint;
import com.example.springboot.service.SelfServicePointService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/api/self-service-points")
public class SelfServicePointController {

    private static final Logger logger = LoggerFactory.getLogger(SelfServicePointController.class);

    @Autowired
    private SelfServicePointService selfServicePointService;

    @GetMapping
    public ResponseEntity<List<SelfServicePoint>> getSelfServicePoints(HttpServletRequest request) {
        String role = request.getHeader("Role");
        String username = request.getHeader("Username");

        logger.info("Fetching self-service points for role: {}, username: {}", role, username);
        List<SelfServicePoint> points = selfServicePointService.getSelfServicePoints(role, username);
        return ResponseEntity.ok(points);
    }

    @PostMapping(consumes = {"multipart/form-data"})
    public ResponseEntity<SelfServicePoint> createSelfServicePoint(
            @RequestPart("dto") SelfServicePointDTO dto,
            @RequestPart(value = "poster", required = false) MultipartFile posterFile,
            HttpServletRequest request) throws Exception {
        String role = request.getHeader("Role");
        String username = request.getHeader("Username");

        logger.info("Creating self-service point for role: {}, username: {}", role, username);
        SelfServicePoint point = selfServicePointService.createSelfServicePoint(dto, posterFile, role, username);
        return ResponseEntity.ok(point);
    }

    @PutMapping(value = "/{pointId}", consumes = {"multipart/form-data"})
    public ResponseEntity<SelfServicePoint> updateSelfServicePoint(
            @PathVariable Integer pointId,
            @RequestPart("dto") SelfServicePointDTO dto,
            @RequestPart(value = "poster", required = false) MultipartFile posterFile,
            HttpServletRequest request) throws Exception {
        String role = request.getHeader("Role");
        String username = request.getHeader("Username");

        logger.info("Updating self-service point {} for role: {}, username: {}", pointId, role, username);
        SelfServicePoint point = selfServicePointService.updateSelfServicePoint(pointId, dto, posterFile, role, username);
        return ResponseEntity.ok(point);
    }

    @DeleteMapping("/{pointId}")
    public ResponseEntity<Void> deleteSelfServicePoint(
            @PathVariable Integer pointId,
            HttpServletRequest request) {
        String role = request.getHeader("Role");
        String username = request.getHeader("Username");

        logger.info("Deleting self-service point {} for role: {}, username: {}", pointId, role, username);
        selfServicePointService.deleteSelfServicePoint(pointId, role, username);
        return ResponseEntity.noContent().build();
    }
}